package com.gexin.controller.system.shop;

import com.gexin.common.AppGlobal;
import com.gexin.core.web.SystemBaseController;
import com.gexin.model.shop.ShopFile;
import com.gexin.model.shop.ShopFileTag;
import com.gexin.model.shop.ShopInfo;
import com.gexin.plugin.sqlinxml.SqlKit;
import com.jfinal.ext.route.ControllerBind;

import java.util.List;

/**
 * 店铺图片Api接口
 * @author 肖德明
 *  2016-06-29
 *
 */
@ControllerBind(controllerKey = "/system/file/api", viewPath = "WEB-INF/system/")
public class ShopFileApiManageController extends SystemBaseController {
	
	public void index(){

	}
	//获取文件列表  /shangjia/file/api?tag_id=?&pagesize=?
	public void list(){
		Integer  tag_id= getParaToInt("tag_id", 1);
		Integer  pagesize= getParaToInt("pagesize", 20);
		List<ShopFile>  list  = ShopFile.dao.find(SqlKit.sql("file.getFileListByShopIdAndTagId"), ShopInfo.shop_id,tag_id,(getpage()-1)*pagesize,pagesize);
		if(list.size()>0){
			success(list);
		}else{
			fail("暂无数据！");
		}
	}
	
	//根据店铺ID查询图片list返回带了总数
	public void categoryList(){
		success(ShopFileTag.dao.find(String.format(SqlKit.sql("file.getFileTageByShopId"), ShopInfo.shop_id), ShopInfo.shop_id));
	}
	
	//上传文件
	public void upfile(){
		String filename=getPara("filename");
		Integer width =getParaToInt("width",0);
		Integer  heigth =getParaToInt("height",0);
		String mime  = getPara("mime");
		Integer  tag_id =getParaToInt("tag_id");
		ShopFile file =new ShopFile();
		file.setAttrHeigth(heigth);
		file.setAttrWidth(width);
		file.setFileName(filename);
		file.setShopId(ShopInfo.shop_id);
		file.setTagId(tag_id);
		file.setUrl(AppGlobal.Qiniu_Addr+filename);
		file.setAttrMimeType(mime);
		if(file.save()==true){
			success(file);
		}else{
			fail("保存失败请稍后再试！");
		}
	}
	
	
}


